package com.zjl.hiring.talent.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zjl.hiring.common.Constant;
import com.zjl.hiring.common.util.AppUtil;
import com.zjl.hiring.talent.dao.ResumeDao;
import com.zjl.hiring.talent.entity.ResumeEntity;

public class ResumeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ResumeDao resumeDao = new ResumeDao();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("deleteResume".equals(command)) {
			deleteResume(request, response);
		} else if ("add".equals(command)) {
			add(request, response);
		} else if ("preUpdate".equals(command)) {
			preUpdate(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else {
			throw new RuntimeException("请求失败！");
		}
	}

	/**
	 * 删除简历
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void deleteResume(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 获取参数
		String resumeId = request.getParameter("resumeId");
		// 调用业务逻辑
		int row = resumeDao.deleteResume(resumeId);
		if (row == 1) {
			response.sendRedirect("TalentServlet?command=main&result="
					+ Constant.OPERATE_SUCCESS);
		} else {
			response.sendRedirect("TalentServlet?command=main&result="
					+ Constant.OPERATE_ERROR);
		}
	}

	/**
	 * 新增简历
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		// 获取参数
		String intention = request.getParameter("intention");
		String workExperience = request.getParameter("workExperience");
		String projectExperience = request.getParameter("projectExperience");
		ResumeEntity resume = new ResumeEntity();
		resume.setTalentId(AppUtil.getTalentId(request, response));
		resume.setIntention(intention);
		resume.setWorkExperience(workExperience);
		resume.setProjectExperience(projectExperience);
		// 调用业务逻辑
		int row = resumeDao.add(resume);

		// 转向
		if (row == 1) {
			// 成功-->main.jsp
			response.sendRedirect("TalentServlet?command=main");
		} else {
			// 失败-->resume-add.jsp
			request.setAttribute("resume", resume);
			request.setAttribute("result", Constant.OPERATE_ERROR);
			request.getRequestDispatcher("view/talent/resume-add.jsp").forward(
					request, response);
		}
	}

	/**
	 * 简历修改前的查询
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void preUpdate(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		// 获得参数
		int resumeId = Integer.parseInt(request.getParameter("resumeId"));
		// 调用业务逻辑
		ResumeEntity resume = resumeDao.get(resumeId);
		// 转向
		if (resume == null) {
			response.sendRedirect("TalantServlet?command=main&result="
					+ Constant.OPERATE_ERROR);
		} else {
			request.setAttribute("resume", resume);
			request.getRequestDispatcher("/view/talent/resume-update.jsp")
					.forward(request, response);
		}
	}

	/**
	 * 修改简历
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void update(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		// 获取参数
		int resumeId = Integer.parseInt(request.getParameter("resumeId"));
		String intention = request.getParameter("intention");
		String workExperience = request.getParameter("workExperience");
		String projectExperience = request.getParameter("projectExperience");
		ResumeEntity resume = new ResumeEntity();
		resume.setId(resumeId);
		resume.setIntention(intention);
		resume.setWorkExperience(workExperience);
		resume.setProjectExperience(projectExperience);
		// 调用业务逻辑
		int row = resumeDao.update(resume);
		// 转向
		if (row == 1) {
			request.setAttribute("result", Constant.OPERATE_SUCCESS);
		} else {
			request.setAttribute("result", Constant.OPERATE_ERROR);
		}
			request.setAttribute("resume", resume);
			request.getRequestDispatcher("view/talent/resume-update.jsp")
					.forward(request, response);
	}
}
